﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataClass
{

    public class ASSqlServer
    {


        public ASSqlServer()
        {
        }


        public static SqlConnection sqlConnection = null;


        private Exception asException = null;
        public Exception Exception { get { return asException; } }

        public bool OpenConnection()
        {
            bool connectionStatus = false;
            try
            {
                //Khai bao va khoi tao doi tuong ASConfig

                ASConfig asConfig = new ASConfig();
                string connectionString = "";
                //Nếu người sử dụng đăng nhập theo đặc quyền Windowns
                if (ASParameters.windowsOnly)
                    connectionString = asConfig.GetConnectionString();
                else
                    connectionString = asConfig.GetConnectionString(ASParameters.userName, ASParameters.passWord);
                //Khai bao va khoi tao doi tuong sqlConnection
                sqlConnection = new SqlConnection(connectionString);
                //khai bao goi phuong thuc Open de mo kt noi csdl
                sqlConnection.Open();
                connectionStatus = true;

            }
            catch (SqlException ex)
            {
                asException = ex;
            }
            catch (Exception ex)
            {
                asException = ex;
            }
            return connectionStatus;
        }



        public void CloseConnection()
        {
            try
            {
                if (sqlConnection.State != ConnectionState.Closed)
                {
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }

            }
            catch (Exception ex)
            {
                asException = ex;
            }
        }


    }

}
